package demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2024-05-04
 * Time: 16:29
 */
public class ReflectClassDemo {

    public static void reflectNewInstance() {
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("demo1.Student");
            Student student = (Student)classStudent.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void reflectPrivateConstructor() {
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("demo1.Student");
            //获得构造方法
            Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("zhaoliu", 15);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateField() {
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("demo1.Student");
            Field field = classStudent.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student)classStudent.newInstance();
            field.set(student,"vo");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateMethod() {
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("demo1.Student");
            Method method = classStudent.getDeclaredMethod("function",String.class);
            method.setAccessible(true);
            Student student = (Student)classStudent.newInstance();
            method.invoke(student,"我是一个反射的参数");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        // reflectPrivateField();
        // reflectPrivateMethod();
    }
    /*
    优点：
      1. 对于任意一个类，都能够知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意一个方法
      2. 增加程序的灵活性和扩展性，降低耦合性，提高自适应能力
      3. 反射已经运用在了很多流行框架如：Struts、Hibernate、Spring
    缺点：
      1. 使用反射会有效率问题。会导致程序效率降低。
      2. 反射技术绕过了源代码的技术，因而会带来维护问题。反射代码比相应的直接代码更复杂
     */
}
